home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 January: Mac OS SDK / Dev.CD Jan 97 SDK2.toast / Development Kits (Disc 2) / OpenDoc Development Framework / ODFDev / ODF / Found / ODUtils / FlipEnd.h < prev    next >
Encoding:
C/C++ Source or Header  |  1996-09-17  |  5.0 KB  |  156 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        FlipEnd.h
  3.  
  4.     Contains:    routines to manipulate endianness of memory
  5.  
  6.     Owned by:    David McCusker
  7.  
  8.     Copyright:    © 1995 by Apple Computer, Inc., all rights reserved.
  9.  
  10.     To Do:
  11.         Perhaps write assembler inlines for ODFlipShort() and ODFlipLong()
  12.         on the Mac platform.
  13.     In Progress:
  14.         
  15. */
  16.  
  17. #ifndef _FLIPEND_
  18. #define _FLIPEND_ 1
  19.  
  20. #ifndef _ODTYPES_
  21. #include <ODTypes.h>
  22. #endif
  23.  
  24.  
  25. #ifdef __cplusplus
  26. extern "C" {
  27. #endif
  28.  
  29. /* _PLATFORM_BIG_ENDIAN_ should be defined for big endian platforms
  30.  * near other platform related defines (e.g. see ODTypesM.idl)
  31.  */
  32.  
  33. /*=========================================================================*/
  34. /* 2 byte endianness flipping */
  35. /*=========================================================================*/
  36.  
  37. ODUShort ODFlipShort(ODUShort n);
  38.     /* n is a single 2-byte int; return it with flipped endianness */
  39.  
  40. void ODFlipShortArray(ODUShort* a, unsigned long count);
  41.     /* a points to count 2-byte ints; flip each int's endianness */
  42.  
  43. /*=========================================================================*/
  44. /* 4 byte endianness flipping */
  45. /*=========================================================================*/
  46.  
  47. ODULong ODFlipLong(ODULong n);
  48.     /* n is a single 4-byte int; return it with flipped endianness */
  49.  
  50. void ODFlipLongArray(ODULong* a, unsigned long count);
  51.     /* a points to count 4-byte ints; flip each int's endianness */
  52.  
  53. /*=========================================================================*/
  54. /* structure endianness flipping */
  55. /*=========================================================================*/
  56.  
  57. void ODFlipStruct(void* structure, const short* groups);
  58.     /* Invert the endianness of the contents of memory in structure
  59.      * according to the layout described by groups, which should
  60.      * be a zero-terminated array of shorts, where each short describes
  61.      * the size of the next chunk of memory in structure to be processed.
  62.      * A negative value -x in the groups array indicates a block
  63.      * of endianness-neutral memory, like a string, and causes x bytes
  64.      * of memory to be skipped over.  A positive value x in the groups
  65.      * array indicates an x byte block of memory which should have its
  66.      * bytes flipped end for end.  Only positive values in the set
  67.      * { 2, 4, 8 } are handled. (Other positive values are handled like
  68.      * negative values: space is skipped). Example:
  69.      *
  70.      * struct Foo {
  71.      *     long  beta;
  72.      *     char  gamma[8];
  73.      *     long  delta;
  74.      *     short alpha;
  75.      * };
  76.      * const short fooGroups[] = {
  77.      *     4, // beta
  78.      *    -8, // gamma 
  79.      *     4, // delta
  80.      *     2, // alpha
  81.      *     0, // zero-termination
  82.      * };
  83.      */
  84.  
  85. /*=========================================================================*/
  86. /* macros for conversion to and from standard (little endian) format */
  87. /*=========================================================================*/
  88.  
  89. #ifdef _PLATFORM_BIG_ENDIAN_
  90.     
  91. #define ConvertODUShortToStd(n)        ODFlipShort(n)
  92. #define ConvertODUShortFromStd(n)      ODFlipShort(n)
  93.     
  94. #define ConvertODSShortToStd(n)        ((ODSShort) ODFlipShort((ODUShort) n))
  95. #define ConvertODSShortFromStd(n)      ((ODSShort) ODFlipShort((ODUShort) n))
  96.     
  97. #define ConvertODULongToStd(n)         ODFlipLong(n)
  98. #define ConvertODULongFromStd(n)       ODFlipLong(n)
  99.         
  100. #define ConvertODSLongToStd(n)         ((ODSLong) ODFlipLong((ODULong) n))
  101. #define ConvertODSLongFromStd(n)       ((ODSLong) ODFlipLong((ODULong) n))
  102.  
  103. #define ConvertODStructToStd(s, g)     ODFlipStruct((s),(g))
  104. #define ConvertODStructFromStd(s, g)   ODFlipStruct((s),(g))
  105.  
  106. #define ConvertODUShortArrayToStd(a,c)    ODFlipShortArray((a),(c))
  107. #define ConvertODUShortArrayFromStd(a,c)  ODFlipShortArray((a),(c))
  108.  
  109. #define ConvertODSShortArrayToStd(a,c)    ODFlipShortArray((ODUShort*)(a),(c))
  110. #define ConvertODSShortArrayFromStd(a,c)  ODFlipShortArray((ODUShort*)(a),(c))
  111.  
  112. #define ConvertODULongArrayToStd(a,c)     ODFlipLongArray((a),(c))
  113. #define ConvertODULongArrayFromStd(a,c)   ODFlipLongArray((a),(c))
  114.  
  115. #define ConvertODSLongArrayToStd(a,c)     ODFlipLongArray((ODULong*)(a),(c))
  116. #define ConvertODSLongArrayFromStd(a,c)   ODFlipLongArray((ODULong*)(a),(c))
  117.  
  118. #else
  119.     
  120. #define ConvertODUShortToStd(n)        (n)
  121. #define ConvertODUShortFromStd(n)      (n)
  122.     
  123. #define ConvertODSShortToStd(n)        (n)
  124. #define ConvertODSShortFromStd(n)      (n)
  125.     
  126. #define ConvertODULongToStd(n)         (n)
  127. #define ConvertODULongFromStd(n)       (n)
  128.         
  129. #define ConvertODSLongToStd(n)         (n)
  130. #define ConvertODSLongFromStd(n)       (n)
  131.         
  132. #define ConvertODStructToStd(s, g)     /* do nothing */
  133. #define ConvertODStructFromStd(s, g)   /* do nothing */
  134.  
  135. #define ConvertODUShortArrayToStd(a,c)    /* do nothing */
  136. #define ConvertODUShortArrayFromStd(a,c)  /* do nothing */
  137.  
  138. #define ConvertODSShortArrayToStd(a,c)    /* do nothing */
  139. #define ConvertODSShortArrayFromStd(a,c)  /* do nothing */
  140.  
  141. #define ConvertODULongArrayToStd(a,c)     /* do nothing */
  142. #define ConvertODULongArrayFromStd(a,c)   /* do nothing */
  143.  
  144. #define ConvertODSLongArrayToStd(a,c)     /* do nothing */
  145. #define ConvertODSLongArrayFromStd(a,c)   /* do nothing */
  146.  
  147. #endif /* _PLATFORM_BIG_ENDIAN_ */
  148.  
  149.  
  150. #ifdef __cplusplus
  151. }
  152. #endif
  153.  
  154. #endif
  155. /* _FLIPEND_ */
  156.